{include file="public/header"}
<body>
<style>
    .wrap{
        width: 96%;
        margin: 1em auto;
        overflow: hidden;
        text-align: center;
    }
</style>
<fieldset class="layui-elem-field">
    <legend>素材列表</legend>
    <div class="layui-field-box">
        <form id="seachForm" method="" action="">
            <div class="layui-inline">
                <label class="layui-form-label">素材标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" value="" placeholder="如：和诚" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">按类型</label>
                <div class="layui-input-inline">
                    <input type="text" name="type" value="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <span id="seach" class="layui-btn layui-btn-normal"><i class="layui-icon">&#xe615;</i> 按条件收索</span>
            <span id="load" class="layui-btn">显示全部</span>
        </form>

        <table class="layui-table" lay-even>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>素材标题</th>
                    <th>类型</th>
                    <th>发布者</th>
                    <th>创建时间</th>
                    <th>手机预览</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody  id="searchContent">
                {volist name="list" id="vo"}
                    <tr>
                        <td>{$vo['id']}</td>
                        <td>{$vo['title']}</td>
                        <td>{$vo['type']}</td>
                        <td>{$vo['author_name']}</td>
                        <td>{$vo['create_time']}</td>
                        <td><span onclick="preview({$vo['id']})" class="layui-btn layui-btn-normal">预览</span></td>
                        <td>
                            <span onclick="edit({$vo['id']})" class="layui-btn">编辑</span>
                            <span onclick="del({$vo['id']})" class="layui-btn layui-btn-danger">删除</span>
                        </td>
                    </tr>
                {/volist}
            </tbody>
        </table>

        <div>{$list->render()}</div>
    </div>
</fieldset>


{include file="public/footer"}
<script>
    //编辑
    function edit(id) {
        window.location.href = '/admin/article/addBoth/id/'+id;
    }

    //删除
    function del(id) {
        layer.confirm('你确定要删除该素材吗?', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var params  = {
                url : "{:url('del')}",
                data: {del_id:id},
                type: 'post',
                sCallback:function(res){
                    layer.msg(res.msg,{time:1800},function(){
                        location.reload();
                    });
                }
            };
            common.request(params);
        });
    }

    //预览
    function preview(id) {
        var params  = {
            url : "{:url('preview')}",
            data: {'id':id},
            type: 'post',
            sCallback:function(res){
                console.log(res.data);
                layer.open({
                    type: 1,
                    title: '手机端预览',
                    area: ['380px', '100%'],
                    maxmin: true,
                    content: '<div class="wrap">'+res.data.content+'</div>'
                });
            }
        };
        common.request(params);
    }

    //收索
    $('#seach').on('click', function () {
        var params  = {
            url : "{:url()}",
            data: $("#seachForm").serialize(),
            type: 'post',
            sCallback:function(res){
                var travelHtml = [];
                for (var i=0; i < res.data.length; i++) {
                    var id = res.data[i]["id"];
                    travelHtml +='<tr> ' + '<td>'+res.data[i]["id"]+'</td> ' +
                            '<td>'+res.data[i]["title"]+'</td> ' +
                            '<td>'+res.data[i]["type"]+'</td> ' +
                            '<td>'+res.data[i]["author_name"]+'</td> ' +
                            '<td>'+res.data[i]["create_time"]+'</td> ' +
                            '<td>'+'<span onclick="preview('+ id +')" class="layui-btn preview layui-btn-normal">预览</span>'+'</td> ' +
                            '<td> ' +
                            '<span onclick="edit('+ id +')"  class="layui-btn">编辑</span>' +
                            '<span onclick="del('+ id +')" class="layui-btn layui-btn-danger">删除</span> ' +
                            '</td> ' + '</tr>';
                };
                $('#searchContent').html(travelHtml);
            }
        };
        common.request(params);
    });

    $('#load').click(function () {
        location.reload();
    });


    layui.use(['layer'], function() {});
</script>
</body>
</html>